<template>
  <el-tree :data="menus" :props="defaultProps" :node-key="defaultProps.id">
    <span class="custom-tree-node" slot-scope="{ node, data }">
      <span>{{ node.label }}</span>
      <span>
        <el-button
          type="text"
          size="mini"
          @click="() => append(data)"
          v-if="data.children.length > 0"
        >
          新增
        </el-button>
        <el-button
          type="text"
          size="mini"
          @click="() => remove(node, data)"
          v-if="node.childNodes.length === 0"
        >
          删除
        </el-button>
      </span>
    </span></el-tree
  >
</template>

<script>
export default {
  data() {
    return {
      menus: [],
      defaultProps: {
        children: "children",
        label: "name",
        id: "catId",
      },
    };
  },
  created() {
    this.getMenus();
  },
  methods: {
    append(data) {
      console.log(data, "data");
    },

    remove(node, data) {
      console.log(node, "node");
      console.log(data, "data");
    },
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/product/category/listTree"),
        method: "get",
      }).then(({ data }) => {
        this.menus = data.data;
      });
    },
  },
};
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>